﻿using Dapper;
using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gear.ORM.Core
{
    [AppStartup(1000)]
    public class ORMStartup : AppStartup
    {
        private List<ConnectionConfig> SqlSugarconnectConfigList { get; }
        public ORMStartup()
        {
            SqlSugarconnectConfigList = ORMTool.SqlSugarConnectionConfigs();
        }
        /// <summary>
        /// ExamKingCore ConfigureServices
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {

            // =====SqlSugar 配置=====
            services.AddSqlSugar(SqlSugarconnectConfigList.ToArray(),
                db =>
                {
                    //处理日志事务
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));
                        Console.WriteLine();
                        App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                    };
                });

            // =====Dapper 配置=====
            services.AddDapper("server=47.108.238.160;port=3306;database=gearadmin;user=gear;password=connection;sslMode=None", SqlProvider.MySql);
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

        }
    }
}
